package com.gitee.wsl.ext.string

/**
 * Removes extra whitespaces in the string. It replaces each sequence of two or more whitespaces with a single one.
 *
 * @param str the string from which to remove extra whitespaces
 * @return the string without extra whitespaces
 */
fun String.normalizeWhitespaces( ): String {
    val sb = StringBuilder()
    for (i in 0 until length) {
        if (i < length - 1 && this[i] == ' ' && this[i + 1] == ' ') {
            continue
        }
        sb.append(this[i])
    }
    return sb.toString()
}